package com.yb.core.bean;

import com.yb.core.constant.HttpConstant;
import com.yb.core.exception.YbBusinessException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/11/12 21:56
 * FileName: Resp
 * Description:
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Resp {
    private Integer code;
    private String msg;
    private Object data;

    public Resp(Integer code){
        this.code=code;
    }
    public static Resp ok(){
        return new Resp().code(HttpConstant.Response.SUCCESS.getCode()).msg(HttpConstant.Response.SUCCESS.getMsg());
    }

    public static Resp error(){
        return new Resp(HttpConstant.Response.ERROR.getCode());
    }

    public static Resp error(YbBusinessException e) {
        return new Resp().code(e.getCode()).msg(e.getMsg()).data(e.getData());
    }


    public Resp code(Integer code){
        this.code=code;
        return this;
    }

    public Resp msg(String msg){
        this.msg=msg;
        return this;
    }

    public Resp data(Object data){
        this.data=data;
        return this;
    }

    public static Resp ok(HttpConstant.Response response){
        return new Resp(response.getCode(), response.getMsg(), null);
    }

    public static Resp ok(HttpConstant.Response response,Object data){
        return new Resp(response.getCode(), response.getMsg(), data);
    }

    public static Resp error(HttpConstant.Response response){
        return new Resp(response.getCode(), response.getMsg(), null);
    }

    public static Resp error(HttpConstant.Response response,Object data){
        return new Resp(response.getCode(), response.getMsg(), data);
    }
}
